package com.gxuwz.prrs.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.adapter.AppointmentRecordAdapter;
import com.gxuwz.prrs.bean.*;
import com.gxuwz.prrs.constant.Constants;
import com.gxuwz.prrs.service.impl.AppointmentRecordServiceImpl;
import com.gxuwz.prrs.utils.NavigationUtils;
import com.gxuwz.prrs.utils.PageUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 张钧
 * 预约记录页
 */
public class AppointmentRecordsActivity extends AppCompatActivity {
	List<Button> buttonList = new LinkedList<>();

	AppointmentRecordAdapter appointmentRecordAdapter;
	ImageView left;
	ImageView right;
	long current = 1;
	long size = 12;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_appointment_record);
		NavigationUtils.setRedirectBottom(this);
		left = findViewById(R.id.left);
		right = findViewById(R.id.right);
		ImageView back = findViewById(R.id.back);
		back.setOnClickListener(view -> NavigationUtils.redirectToMainActivity(AppointmentRecordsActivity.this,null));
		PageUtils.setupPageButtons(this, buttonList, this::onClick);
		List<AppointmentRecord> appointmentRecords = loadPage(1);
		if (appointmentRecords != null) {
			appointmentRecordAdapter = new AppointmentRecordAdapter(this, appointmentRecords);
			ListView listView = findViewById(R.id.appointment_list_view);
			listView.setAdapter(appointmentRecordAdapter);
			listView.setOnItemClickListener((adapterView, view, i, l) -> {
				AppointmentRecord item = appointmentRecordAdapter.getItem(i);
				Bundle bundle = new Bundle();
				bundle.putString(Constants.APPOINTMENT_DATE,item.getAppointmentDate());
				bundle.putString(Constants.CHECK_IN_TIME,item.getCheckInTime());
				bundle.putString(Constants.CHECK_OUT_TIME,item.getCheckOutTime());
				bundle.putInt(Constants.PIANO_ROOM_NO,item.getPianoRoomNo());
				bundle.putString(Constants.PIANO_APPOINT_TIME,item.getPianoAppointTime());
				bundle.putString(Constants.STATUS,item.getStatus());
				bundle.putString(Constants.APPOINTMENT_TIME,item.getAppointmentTime());
				NavigationUtils.redirectToAppointmentRecordDetailActivity(AppointmentRecordsActivity.this,bundle);
			});
		}
		Page<AppointmentRecord> page = new Page<>();
		page.setSize(size);
		PageUtils.setupNavigationButtons(this, buttonList, left, right, current, appointmentRecordAdapter, AppointmentRecordServiceImpl.class, page);
	}

	/**
	 * 按钮点击事件
	 *
	 * @param view 按钮
	 */
	private void onClick(View view) {
		Button button = (Button) view;
		long aim = Long.parseLong(String.valueOf(button.getText()));
		appointmentRecordAdapter.updateData(loadPage(aim));
	}

	/**
	 * 加载页面
	 *
	 * @param current 目标页
	 * @return 页面数据
	 */
	private List<AppointmentRecord> loadPage(long current) {
		Page<AppointmentRecord> p = new Page<>();
		p.setSize(size);
		List<AppointmentRecord> appointmentRecords = PageUtils.loadPage(this, current, appointmentRecordAdapter, AppointmentRecordServiceImpl.class, p, buttonList, left, right);
		this.current = current;
		return appointmentRecords;
	}
}
